#include <myheader.h>
#include <pthread.h>
#include <mutex>
#include <condition_variable>

pthread_mutex_t mux;
pthread_cond_t condb;
pthread_cond_t conda;

int bready = 0;
int aready = 0;
void * func1(void *arg)
{
    sleep(3);

    printf("A\n");
    aready= 1;
    pthread_cond_signal(&conda);
    pthread_mutex_lock(&mux);
    while(0 == bready)
    {
        pthread_cond_wait(&condb,&mux);
    }
    pthread_mutex_unlock(&mux);
    printf("C\n");
    pthread_exit(NULL);
    
}

void * func2(void *arg)
{
    pthread_mutex_lock(&mux);
    while(0 == aready)
    {
        pthread_cond_wait(&conda,&mux);
    }
    pthread_mutex_unlock(&mux);       
    sleep(3);
    printf("B\n");
    bready = 1;
    pthread_cond_signal(&condb);
    pthread_exit(NULL);
}
/*Usage:  */
int main(int argc, char *argv[])
{
    //创建两个线程
    pthread_t pid1, pid2;
    pthread_create(&pid1, NULL, func1, NULL);
    pthread_create(&pid2, NULL, func2, NULL);

    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);
    pthread_mutex_destroy(&mux);
    pthread_cond_destroy(&conda);
    pthread_cond_destroy(&condb);

    return 0;
}

